package xin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import xin.entity.TAuthPermission;
import xin.entity.TAuthRolePermission;
import xin.dao.TAuthRolePermissionMapper;
import xin.service.TAuthRolePermissionService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author young
 * @since 2023年10月09日
 */
@Service
public class TAuthRolePermissionServiceImpl extends ServiceImpl<TAuthRolePermissionMapper, TAuthRolePermission> implements TAuthRolePermissionService {

    @Override
    public Integer addPermission(Long roleId, List<String> list) {
        //先删除全部数据
        this.baseMapper.delete(new QueryWrapper<TAuthRolePermission>().eq("role_id",roleId));
        for (String s : list) {
            TAuthRolePermission tAuthRolePermission = new TAuthRolePermission();
            tAuthRolePermission.setRoleId(roleId);
            tAuthRolePermission.setPermissionSn(s);
            this.save(tAuthRolePermission);
        }
        return 1;
    }

    /**
     * 查询用户拥有的权限id
     * @param roleId
     * @return
     */
    @Override
    public List<String> findUserHasPermission(Long roleId) {
        List<TAuthRolePermission> list = this.baseMapper.selectList(new QueryWrapper<TAuthRolePermission>().eq("role_id", roleId));
        return list.stream().map(item->item.getPermissionSn()).collect(Collectors.toList());
    }
}
